package com.fh.alipay.controller;

import com.fh.alipay.annotation.Authorize;
import com.fh.alipay.vo.ApiResult;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.fh.alipay.po.Merchant;
import com.fh.alipay.service.IMerchantService;
import com.fh.alipay.model.dto.MerchantDTO;
import com.fh.alipay.model.vo.MerchantVO;
import com.fh.alipay.model.query.MerchantQuery;
import com.fh.alipay.model.convert.MerchantConvert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.Arrays;

import org.springframework.web.bind.annotation.RestController;

/**
 * @author fh
 * @date 2022-05-02
 * @description 控制器
 */
@RestController
@RequestMapping("/aliPay/merchant")
@Transactional(rollbackFor = Exception.class)
public class MerchantController extends BaseController {

    @Resource
    private IMerchantService merchantService;


    /**
     * 获取分页列表
     */
    @PostMapping(value = "/list")
//    @Authorize
    public ApiResult<IPage<Merchant>> list(MerchantQuery query, Page page) {
        IPage<Merchant> resultPage = merchantService.getWithPage(page, query);
        return ApiResult.ok(resultPage);
    }

    /**
     * 根据ID获取详情
     */
    @GetMapping("/get")
    public ApiResult<MerchantVO> get(@RequestParam("id") Long id) {
        Merchant entity = merchantService.getById(id);
        MerchantVO vo = MerchantConvert.toVO(entity);
        return ApiResult.ok(vo);
    }

    /**
     * 编辑
     */
    @PostMapping("/update")
//    @Authorize
    public ApiResult update(@Valid MerchantDTO dto, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return ApiResult.error("参数不能为空");
        }

        Merchant po = MerchantConvert.toPO(dto);
        po.setUpdateTime(LocalDateTime.now());
        boolean update = merchantService.updateById(po);
        return update ? ApiResult.ok() : ApiResult.fail();
    }

}
